package convert

import (
	"errors"
	"math"
	"strconv"
)

// Float64ToInt 将 float64 转换为 int，检查溢出
func Float64ToInt(value float64) (int, error) {
	if value < float64(math.MinInt) || value > float64(math.MaxInt) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int(value), nil
}

// Float64ToInt8 将 float64 转换为 int8，检查溢出
func Float64ToInt8(value float64) (int8, error) {
	if value < float64(math.MinInt8) || value > float64(math.MaxInt8) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int8(value), nil
}

// Float64ToInt16 将 float64 转换为 int16，检查溢出
func Float64ToInt16(value float64) (int16, error) {
	if value < float64(math.MinInt16) || value > float64(math.MaxInt16) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int16(value), nil
}

// Float64ToInt32 将 float64 转换为 int32，检查溢出
func Float64ToInt32(value float64) (int32, error) {
	if value < float64(math.MinInt32) || value > float64(math.MaxInt32) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int32(value), nil
}

// Float64ToInt64 将 float64 转换为 int64，检查溢出
func Float64ToInt64(value float64) (int64, error) {
	if value < float64(math.MinInt64) || value > float64(math.MaxInt64) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int64(value), nil
}

// Float64ToUint 将 float64 转换为 uint，检查溢出
func Float64ToUint(value float64) (uint, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint")
	}
	if value > float64(math.MaxUint) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint(value), nil
}

// Float64ToUint8 将 float64 转换为 uint8，检查溢出
func Float64ToUint8(value float64) (uint8, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint8")
	}
	if value > float64(math.MaxUint8) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint8(value), nil
}

// Float64ToUint16 将 float64 转换为 uint16，检查溢出
func Float64ToUint16(value float64) (uint16, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint16")
	}
	if value > float64(math.MaxUint16) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint16(value), nil
}

// Float64ToUint32 将 float64 转换为 uint32，检查溢出
func Float64ToUint32(value float64) (uint32, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint32")
	}
	if value > float64(math.MaxUint32) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint32(value), nil
}

// Float64ToUint64 将 float64 转换为 uint64，检查溢出
func Float64ToUint64(value float64) (uint64, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint64")
	}
	return uint64(value), nil
}

// Float64ToFloat32 将float64类型转换为float32类型，直接进行类型转换
func Float64ToFloat32(value float64) (float32, error) {
	// 直接将float64类型的值转换为float32类型
	// 注意：这种转换可能会丢失精度，因为float32的精度低于float64
	return float32(value), nil
}

// Float64ToString 将 float64 转换为 string
func Float64ToString(value float64) string {
	return strconv.FormatFloat(value, 'f', -1, 64)
}

// Float64ToBool 将 float64 转换为 bool
func Float64ToBool(value float64) bool {
	return value != 0.0
}
